var that;
var city = "杭州";
Page({

  /**
   * 页面的初始数据
   */

  data: {
    lists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    wx.getLocation({
      success: function(res) {
        console.log(res);
        var longitude = res.longitude
        var latitude = res.latitude
        that.loadCity(longitude, latitude)
      },
      fail:function(res) {
        console.log("失败");
        that.loadList(city);
      }
    })
  },
  loadCity: function (longitude, latitude) {
     that = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=pOeOZLMQsVDiReMnevEnqFPKu73bydK1&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // success  
        console.log(res);
        var city = res.data.result.addressComponent.city;
        that.setData ({
          city:city
        });
        wx.setNavigationBarTitle({
          title: city+"天气预报",  
        })
        navigationBarTitleText: city;
        console.log(city);
        that.loadList(city);
      },
      fail: function () {
        console.log("获取定位失败");
      },
    })
  },
  loadList: function (city) {
    wx.showLoading({
      title: "加载中...",
    })
    that = this;
    wx.request({
      url: 'https://www.apiopen.top/weatherApi',
      data: {
        "city": city,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        wx.stopPullDownRefresh();
        console.log(res.data);
        var list = res.data.data.forecast;
        that.setData({
          lists: list,
        });
      },
      fail: function (res) {
        console.log(res);
        wx.stopPullDownRefresh();
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})